--[[

YodaCrypt v0.1

Encrypts public and private chat messages.

Commands:

* Enable/disable the encryption
!yodacrypt [on|off]

* Set/change the password
!yodacrypt password [password]

]]

-- Main Configuration
cfg = {
	-- Flag for beginning of chat - if this is at the beginning of a chat line, it will be assumed this is an encrypted message.
	flag = "enc§",
	
	-- Is the plugin enabled?
	enabled = false,
	
	-- The password for encryption
	password = "chocolatecake",
}

-- User Commands
cmds = {
	on = function(params)
		cfg.enabled = true
	end,
	
	off = function(params)
		cfg.enabled = false
	end,
	
	password = function(params)
		cfg.password = params
	end,
}

OnError = function(msg)
	DC():PrintDebug(msg)
end

function Split(str, delim, maxNb)
	-- Eliminate bad cases...
	if string.find(str, delim) == nil then
		return { str }
	end
	if maxNb == nil or maxNb < 1 then
		maxNb = 0	-- No limit
	end
	local result = {}
	local pat = "(.-)" .. delim .. "()"
	local nb = 0
	local lastPos
	for part, pos in string.gfind(str, pat) do
		nb = nb + 1
		result[nb] = part
		lastPos = pos
		if nb == maxNb then break end
	end
	-- Handle the last field
	if nb ~= maxNb then
		result[nb + 1] = string.sub(str, lastPos)
	end
	return result
end

-- intercept chat messages for commands
function dcpp.OnCommandEnter(hub, text)
	local _,_,cmd,params = text:find("[!+//]yodacrypt (%w+)%s?(.*)")	--catch the command
	if cmd and cmds[cmd] then	--if it's a command
		if params then	--parse params into an array, just because I can
			params = Split(params, " ")
		end
		cmd = cmd:lower()
		cmds[cmd](params)
		return -1
	else
		if cfg.enabled == true then
			return -1
			--DC():SendHubMessage( hub:getId(), "<" .. hub:getOwnNick() .. "> "..YodaCrypt(text).."|" )
		end
	end
end


